package com.yc.bbs;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.yc.bbs.controller.Loginzinterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.sql.DataSource;
import java.util.List;

@Configuration
@ComponentScan("com.yc.bbs")
@EnableWebMvc
@MapperScan("com.yc.bbs.dao")
public class BbsConfig implements WebMvcConfigurer {
    //该配置会导致静态资源不被拦截
    //    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }
        @Bean
        DataSource dataSource(){
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql:///bbs?useSSL=false");
            dataSource.setUsername("root");
            dataSource.setPassword("root");
            return dataSource;
        }
        @Bean
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            /**
             * 加载 mybatis 配置 ( 可选 )
             * setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
             */
            // ClassPathResource classPathResource = new ClassPathResource("mybatis.xml");
            // sqlSessionFactoryBean.setConfigLocation(classPathResource);
            /**
             * MyBatis配置类 等效于 mybatis.xml
             */
            MybatisConfiguration configuration = new MybatisConfiguration();
            /**
             * 配置日志框架 log4j, 等效于
             * <settings>
             *     <setting name="logImpl" value="LOG4J">
             * </setting></settings>
             */
            configuration.setLogImpl(Log4jImpl.class);
            /**
             * MyBatis-Plus 分页插件
             */
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
            paginationInnerInterceptor.setDialect(new MySqlDialect());
            paginationInnerInterceptor.setDbType(DbType.MYSQL);
            mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
            configuration.addInterceptor(mybatisPlusInterceptor);
            /**
             *  setConfiguration 不能和 setConfigLocation 同时使用, 否则报错
             */
            sqlSessionFactoryBean.setConfiguration(configuration);
            return sqlSessionFactoryBean;
        }
    @Bean
    MultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/head/**")
                //file:外部文件路径
                .addResourceLocations(
//                        "file:/Users/xiaoktongxue/Desktop/images/",
//                        "file:/Users/xiaoktongxue/sanqistudy/three-phase-learning/bbs/src/main/webapp/image/head/");
                        "file:/root/bbs/upload/",
                        "/image/head/");
        registry.addResourceHandler("/**")
//                .addResourceLocations("file:/Users/xiaoktongxue/sanqistudy/three-phase-learning/bbs/src/main/webapp/");
                .addResourceLocations("file:/root/gitee/three-phase-learning/bbs/src/main/webapp/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Loginzinterceptor())
                .addPathPatterns("/post.html","/back/**");
        }
}
